layui.config({
	base: '/static/build/js/'
}).use(['tab', 'element', 'navbar'], function() {
	var app = layui.app,
		tab = layui.tab,
		$ = layui.jquery,
		layer = layui.layer,
		navbar = layui.navbar,
		element = layui.element;
	var id = 2;
	tab.set({
		renderType: 'iframe',
		elem: '#container',
		mainUrl: '/menu/main',
		onSwitch: function(data) { //选项卡切换时触发
			id = data.layId;
			//			console.log(); //lay-id值
			//			console.log(data.index); //得到当前Tab的所在下标
			//			console.log(data.elem); //得到当前的Tab大容器
		},
		closeBefore: function(data) { //关闭选项卡之前触发
			//			console.log(data);
			//			console.log(data.icon); //显示的图标
			//			console.log(data.id); //lay-id
			//			console.log(data.title); //显示的标题
			//			console.log(data.url); //跳转的地址
			return true; //返回true则关闭
		}
	}).render();
	navbar.bind(function(data) {
		tab.tabAdd(data);
	});

	username = localStorage.getItem("username"); //本地存储信息；
	loadmenu();

	function loadmenu() {
		var menu = $(".layui-nav-tree");
		$.ajax({
			type: "post",
			url: "/getmenu",
			async: true,
			data: {
				username: username
			},
			success: function(data) {
				var result = $.parseJSON(data);
				var menujson = new Array();
				var menu;
				var children;
				//一级菜单json
				for(var i = 0; i < result.length; i++) {
					if(result[i].parent_menucode == 0) {
						menu = {
							"id": result[i].menu_id,
							"title": result[i].menu_name,
							"icon": result[i].icon,
							"menu_code": result[i].menu_code,
							"spread": true, //为true菜单展开
							"children": []
						}
						menujson.push(menu);
					}
				}
				//二级菜单json
				for(var i = 0; i < menujson.length; i++) {
					for(var j = 0; j < result.length; j++) {
						if(menujson[i].menu_code == result[j].parent_menucode) {
							children = {
								"id": result[j].menu_id,
								"title": result[j].menu_name,
								"icon": result[j].icon,
								"url": result[j].data_url
							}
							menujson[i]["children"].push(children);
						}
					}
				}
				var menuList = menujson;
				navbar.set({
					data: menuList
				}).render(function(data) {
					tab.tabAdd(data);
				});
			}
		});
	}

	function getname() {
		$.ajax({
			type: "post",
			url: "/getname",
			async: true,
			data: {
				username: username
			},
			success: function(data) {
				var result = $.parseJSON(data);
				$(".username").html(result.realname);
				$(".rolename").html(result.rolename);
			}
		});
	};
	getname();
	$(".layui-side-scroll").on('click', 'a', function() {
		var dds = $(".layui-tab-title li").length;
		if(dds >= 7) {
			layer.open({
				content: '打开的选项卡过多，只保留当前选项卡',
				yes: function(index, layero) {
					layer.close(index);
					$(".kit-tab ul.layui-tab-title").children("li[lay-id]").each(function() {
						var ids = $(this).attr("lay-id");
						if(ids != id && ids !== undefined && ids != -1) {
							element.tabDelete('kitTab', ids);
						}
					});
					//如果设定了yes回调，需进行手工关闭
				}
			});
		}
	});
	$("#editPassword").on("click", function() {
		$.ajaxSetup({
			async: false
		});
		layer.open({
			type: 1,
			title: '修改密码',
			content: '<fieldset class="layui-elem-field layui-form">' +
				'<div class="layui-form-item">' +
				'<div class="layui-input-inline">' +
				'<label class="label-position"></label>' +
				'<input id="confirmOldPassword" class="layui-input layui-input-margin" type="password" placeholder="旧密码" value=""/>' +
				'</div>' +
				'</div>' +
				'<div class="layui-form-item">' +
				'<div class="layui-input-inline">' +
				'<label class="label-position"></label>' +
				'<input id="newPassword" class="layui-input" type="password" placeholder="新密码" value="" />' +
				'</div>' +
				'</div>' +
				'<div class="layui-form-item">' +
				'<div class="layui-input-inline">' +
				'<label class="label-position"></label>' +
				'<input id="confirmPassword" class="layui-input" type="password" placeholder="确认密码" value="" />' +
				'</div>' +
				'</div>' +
				'</fieldest>',
			btn: ['确定', '取消'],
			btnAlign: 'c',
			area: ['220px', '280px'],
			success: function() {
				if((navigator.appName == "Microsoft Internet Explorer") && (document.documentMode < 10 || document.documentMode == undefined)) {
					var $placeholder = $("input[placeholder]");
					console.log($placeholder);
					for(var i = 0; i < $placeholder.length; i++) {
						if($placeholder.eq(i).attr("type") == "password") {
							$placeholder.eq(i).siblings("label").text($placeholder.eq(i).attr("placeholder")).show()
						} else {
							$placeholder.eq(i).val($placeholder.eq(i).attr("placeholder")).css({
								"color": "#ccc"
							})
						}
					}
					$placeholder.focus(function() {
						if($(this).attr("type") == "password") {
							$(this).siblings("label").hide()
						} else {
							if($(this).val() == $(this).attr("placeholder")) {
								$(this).val("").css({
									"color": "#ccc"
								})
							}
						}
					}).blur(function() {
						if($(this).attr("type") == "password") {
							if($(this).val() == "") {
								$(this).siblings("label").text($(this).attr("placeholder")).show()
							}
						} else {
							if($(this).val() == "") {
								$(this).val($(this).attr("placeholder")).css({
									"color": "#ccc"
								})
							}
						}
					});
				}
			},
			yes: function() {
				console.log("111");
			}
		});
	});

});